package com.kyub.dynamoscaffold

import org.codehaus.groovy.grails.commons.GrailsDomainClass
import org.codehaus.groovy.grails.commons.GrailsDomainClassProperty

/**
 * Created by IntelliJ IDEA.
 * User: sreghenzi
 * Date: 22-mar-2010
 * Time: 9.44.11
 * To change this template use File | Settings | File Templates.
 */
class ListDomPropertyComparator implements Comparator{

  private List listedProperties

  private GrailsDomainClass domainClass

   public ListDomPropertyComparator(GrailsDomainClass domainClass ,List listedProperties) {
        if(domainClass == null || listedProperties == null)
            throw new IllegalArgumentException("All Arguments required!");
        this.domainClass = domainClass;
       this.listedProperties = listedProperties

   }

   public int compare(Object o1, Object o2) {
        if(o1.equals(domainClass.getIdentifier()))
            return -1;
        else if(o2.equals(domainClass.getIdentifier()))
            return 1;
        else{
          GrailsDomainClassProperty prop1 = (GrailsDomainClassProperty)o1;
          GrailsDomainClassProperty prop2 = (GrailsDomainClassProperty)o2;
          def wprops = listedProperties.indexOf(prop1.name).compareTo(listedProperties.indexOf(prop2.name))
          if(wprops != 0)
            return wprops
          else{
            if(prop1.type == List.class ||prop1.type == Set.class )
              return -1
            else
              return prop1.name.compareTo(prop2.name)
          }

        }



   }


}
